package com.liliudong.pinkyponk.generator;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 代码生成
 *
 * @author liliudong
 * @date 2022-09-23 16:14
 **/
public class PinkyPonkGeneratorApplication {

    private static final String BASE_DIR_URL = "D:/code/pinky-ponk";
    private static final String[] TABLES = {"sys_role_menu"};

    public static void main(String[] args) {
        DataSourceConfig.Builder dataSource = new DataSourceConfig.Builder(DataSourceConfiguration.URL,
                DataSourceConfiguration.USERNAME, DataSourceConfiguration.PASSWORD);

        FastAutoGenerator.create(dataSource)
                .globalConfig(builder -> builder.outputDir(BASE_DIR_URL + "/pinky-ponk-generator")
                        .author("liudong.li")
                        .dateType(DateType.TIME_PACK)
                        .disableOpenDir()
                        .commentDate("yyyy-MM-dd"))
                .packageConfig(builder -> builder.parent("com.liliudong.pinkyponk")
                        .entity("biz.sys.entity")
                        .service("biz.sys.service")
                        .serviceImpl("biz.sys.service.impl")
                        .mapper("biz.sys.mapper")
                        .xml("mapper.xml")
                        .controller("controller")
                        .pathInfo(new HashMap<>(8) {
                            {
                                put(OutputFile.entity, BASE_DIR_URL + "/pinky-ponk-generator/src/main/java/com/liliudong/pinkyponk/biz/sys/entity");
                                put(OutputFile.service, BASE_DIR_URL + "/pinky-ponk-generator/src/main/java/com/liliudong/pinkyponk/biz/sys/service");
                                put(OutputFile.serviceImpl, BASE_DIR_URL + "/pinky-ponk-generator/src/main/java/com/liliudong/pinkyponk/biz/sys/service/impl");
                                put(OutputFile.xml, BASE_DIR_URL + "/pinky-ponk-generator/src/main/resources/mapper");
                                put(OutputFile.mapper, BASE_DIR_URL + "/pinky-ponk-generator/src/main/java/com/liliudong/pinkyponk/biz/sys/mapper");
                                put(OutputFile.controller, BASE_DIR_URL + "/pinky-ponk-generator/src/main/java/com/liliudong/pinkyponk/biz/sys/controller");
                            }
                        }))
                .templateConfig(builder -> builder.entity("/templates/entity.java")
                        .service("/templates/service.java")
                        .serviceImpl("/templates/serviceImpl.java")
                        .mapper("/templates/mapper.java")
                        .controller("/templates/controller.java")
                        .xml("/templates/mapper.xml"))
                .strategyConfig(builder -> builder
                        .addInclude(TABLES)
                        .serviceBuilder()
                        .formatServiceFileName("%sService"))
                .injectionConfig(builder -> builder.beforeOutputFile((tableInfo, objectMap) -> {
                            String entityName = tableInfo.getEntityName();
                            String tableInfoName = tableInfo.getName();
                            int firstIndex = tableInfoName.indexOf("_");
                            String tablePrefix = tableInfoName.substring(0, firstIndex);
                            String tableSuffix = tableInfoName.substring(firstIndex + 1);
                            String[] split = tableSuffix.split("_");
                            String tableBusinessName = StrUtil.join("-", Arrays.asList(split));
                            String entityNameCamelCase = StrUtil.lowerFirst(entityName);
                            Map<String, Object> packageMap = Convert.toMap(String.class, Object.class, objectMap.get("package"));
                            String entityPackage = Convert.toStr(packageMap.get("Entity"));

                            objectMap.put("convertPackage", entityPackage.replaceFirst("entity", "convert"));
                            objectMap.put("requestPackage", entityPackage.replaceFirst("entity", "request"));
                            objectMap.put("responsePackage", entityPackage.replaceFirst("entity", "response"));
                            objectMap.put("tablePrefix", tablePrefix);
                            objectMap.put("tableBusinessName", tableBusinessName);
                            objectMap.put("entityNameCamelCase", entityNameCamelCase);
//                            objectMap.put("packageConvert", packageConvert);
                        })
                        .customFile(ListUtil.of(
                                        new CustomFile.Builder()
                                                .fileName("Response.java")
                                                .templatePath("/templates/response.java.vm")
                                                .filePath(BASE_DIR_URL + "/pinky-ponk-generator/src/main/java/com/liliudong/pinkyponk/biz/sys/response")
                                                .build(),
                                        new CustomFile.Builder()
                                                .fileName("Convert.java")
                                                .templatePath("/templates/convert.java.vm")
                                                .filePath(BASE_DIR_URL + "/pinky-ponk-generator/src/main/java/com/liliudong/pinkyponk/biz/sys/convert")
                                                .build(),
                                        new CustomFile.Builder()
                                                .fileName("Request.java")
                                                .templatePath("/templates/request.java.vm")
                                                .filePath(BASE_DIR_URL + "/pinky-ponk-generator/src/main/java/com/liliudong/pinkyponk/biz/sys/request")
                                                .build()
                                )
                        ))
                .execute();


    }
}
